package com.xxxx.server.test;


import com.google.common.base.Joiner;
import org.junit.Test;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

/**
 * @description:
 * @author: Honors
 * @create: 2021-10-08
 */
public class T1 {

    public static void main(String[] args) {
        System.out.println("请输入字符串：");
        Scanner scanner1 = new Scanner(System.in);
        String s1 = scanner1.next();
        System.out.println("请输入子字符串：");
        Scanner scanner2 = new Scanner(System.in);
        String next = scanner2.next();
        int stringCount = getStringCount(s1, next);
        System.out.println("共有" + stringCount + "次出现");
    }

    public static int getStringCount(String str, String key) {
        //定义计数器
        int count = 0;
        //定义变量,保存indexOf查找后的索引的结果
        int index = 0;
        //开始循环找,条件,indexOf==-1 字符串没有了
        int indexnum = 0;
        while ((index = str.indexOf(key)) != -1) {
            count++;
            // 获取到的索引,和字符串长度求和,截取字符串
            str = str.substring(index + key.length());
            // 获取截取的字符串中key字符串最后一个字母所在位置的下标
            indexnum += (index + key.length());
            System.out.println("第" + count + "次查找" + key + "出现的下标是" + (indexnum - key.length()));
            // 截取的字符串中包含key字符串，所以需要减去key字符串的长度
        }
        return count;//返回出现次数
    }

    @Test
    public void t6() {
        int[] a = {100, 50, 184, 548, 154};
        System.out.println(xx1(a));
    }

    public double xx1(int[] a) {
        double x = 0;
        for (int b : a) {
            x = x + b;
        }
        return x / a.length;
    }


    @Test
    public void t5() {
        int x = 1;
        while (x <= 100) {
            System.out.println(getRandom(5, 6));
            x = x + 1;
        }
    }

    public int getRandom(int min, int max) {
        // 该方法的作用是生成一个随机的int值，该值介于[0,n)的区间，也就是0到n之间的随机int值，包含0而不包含n。
        return new Random().nextInt(max) % (max - min + 1) + min;
    }

    public int sum(int[] arr) {
        int count = 0;
        int count2 = 0;
        for (int i = 0; i < arr.length; i++) {
            count = arr[i];
            count2 += count;
        }
        return count2;
    }

    @Test
    public void t4() {
        int a1 = 0;
        int a2 = 0;
        int a3 = 0;
        int a4 = 0;
        int a5 = 0;
        int a6 = 0;
        int a7 = 0;
        int a8 = 0;
        int a9 = 0;
        int a = 100;
        int b = 0;
        List<Object> list = new ArrayList<>();
        while (b < 100) {
            int x = new Random().nextInt(9) + 1;
            list.add(x);
            b = b + 1;
            if (x == 1) {
                a1 = a1 + 1;
            }
            if (x == 2) {
                a2 = a2 + 1;
            }
            if (x == 3) {
                a3 = a3 + 1;
            }
            if (x == 4) {
                a4 = a4 + 1;
            }
            if (x == 5) {
                a5 = a5 + 1;
            }
            if (x == 6) {
                a6 = a6 + 1;
            }
            if (x == 7) {
                a7 = a7 + 1;
            }
            if (x == 8) {
                a8 = a8 + 1;
            }
            if (x == 9) {
                a9 = a9 + 1;
            }
        }
        System.out.println("100个随机数分别为：" + list);
        System.out.println("1出现了：" + a1 + "次");
        System.out.println("2出现了：" + a2 + "次");
        System.out.println("3出现了：" + a3 + "次");
        System.out.println("4出现了：" + a4 + "次");
        System.out.println("5出现了：" + a5 + "次");
        System.out.println("6出现了：" + a6 + "次");
        System.out.println("7出现了：" + a7 + "次");
        System.out.println("8出现了：" + a8 + "次");
        System.out.println("9出现了：" + a9 + "次");
//        System.out.println(x);
    }

    @Test
    public void t3() {
        String path = Thread.currentThread().getContextClassLoader().getResource("...").getPath();
        System.out.println(path);

        String s = "I am a student";
        System.out.println(s.substring(9, 13));
        System.out.println(s.length());

    }


    @Test
    public void t2() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            // 当前时间时间戳
            long time = new Date().getTime();
            // 结束时间时间戳
            long parse = sdf.parse("2021-10-26").getTime();
            // 当前时间 - 结束时间 = 剩余时间
            long x = (time - parse) / 86400000;
            System.out.println("当前时间" + "距离" + "2021-11-08还有" + x + "天");
            System.out.println(time / 86400000);
            System.out.println(parse / 86400000);
            System.out.println(x);
            if (10 >= x) {
                System.out.println("发送通知");
            } else {
                System.out.println("不发送");
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }


    public String percentageCompletionRate(Long a, Long b) {
        if (a != b) {
            DecimalFormat decimalFormat = new DecimalFormat("0.000");
            // 获取小数点完成率
            String probability = decimalFormat.format(b.doubleValue() / a.doubleValue());
            System.out.println(probability);
            // 精确到 % 小数点后一位 33.3%
            String percentage = Float.parseFloat(probability.substring(probability.lastIndexOf(".") + 1)) / 10 + "%";
            return percentage;
        }
        return "100.0%";
    }

    @Test
    public void t1() {
        List<String> list = new ArrayList<>();
        list.add("张三");
        list.add("李四");
        list.add("王五");
        list.add("赵六");
        String result = Joiner.on(",").join(list);
        System.out.println(result);
        String t2 = "";
        for (int i = 0; i < list.size(); i++) {
            String t1 = list.get(i);
            if (!t2.equals("")) {
                t2 += "," + t1;
            } else {
                t2 += t1;
            }
        }
        System.out.println(t2);

    }
}
